<template>
	<view class="content">
	</view>
</template>

<script>
	import {
		mapState,
	} from 'vuex'
	export default {
		computed: mapState(['baseUrl']),
		data() {
			return {
				title: 'Hello',
				msg: '',
				logoSrcs: ['/static/head.png'],
				type: 0,
				userId: null, //当前登录用户的id
			}
		},
		onLoad(option) {
			this.type = option.type
			this.inter(option.type)
		},
		methods: {
			inter(val) {
				const _self = this;
				const PPFace = uni.requireNativePlugin('PP-BaiduFace');
				let config = {
					licenseName: 'idl-license',
					licenseSuffix: 'face-android',
					licenseId: 'qicq-face-android',
					liveActionArray: ["Eye", "HeadLeft", "HeadLeftOrRight"],
					bgColor: "#000000",
					textColor: "#F0FFFF",
					isSound:false
				}
				
				if (plus.os.name == 'Android') {
					config.licenseSuffix='face-android'
					config.licenseId='qicq-face-android'
				}
				if (plus.os.name == 'iOS') {
					config.licenseSuffix='face-ios'
					config.licenseId='qicq-face-ios'
				}
				PPFace.recycler(config, res => {
					if (res.FaceStatusEnum == '"OK"') {
						if(this.type==1){
							//注册
							this.faceRegist(res.base64ImageMap)
						}else {
							this.faceLogin(res.base64ImageMap)
						}					
						
					}
				});
				
			},
			faceRegist(val){
				var userId = uni.getStorageSync("jaUserId")
				uni.request({
					url: this.baseUrl+'api/face/faceRegister', //仅为示例，并非真实接口地址。
					method: "POST",
					data: {
						base64: val,
						userId: userId
					},
					success: (res) => {
						uni.hideLoading()
						if (res.data.code == 400) {
							//人脸注册失败
							uni.hideLoading()
							uni.showToast({
								title: res.data.desc,
								duration: 3000,
								icon: "none"
							})
							/* 返回用户中心 */
						uni.switchTab({
						    url: '../ucenter/ucenter'
						});	
						}else if(res.data.code==200){
							//人脸注册成功
							uni.hideLoading()
							uni.showToast({
								title: res.data.desc,
								duration: 3000,
								icon: "none"
							})
								uni.switchTab({
								    url: '../ucenter/ucenter'
								});
						}
					},
					fail(e) {
						uni.hideLoading()
						uni.showToast({
							title: "未知错误",
							duration: 2000,
							icon: "none"
				
						})
					}
				});
				
			},
			
			
			faceLogin(val) {
				/* 判断当前是注册还是登录 */
				console.log(this.baseUrl)
				uni.showLoading({
					title: "正在处理",
				})
				uni.request({
					url: this.baseUrl+'api/face/loginByFace', //仅为示例，并非真实接口地址。
					method: "POST",
					data: {
						base64: val,
					},
					success: (res) => {
						uni.hideLoading()
						if (res.data.code == 500) {
							uni.showToast({
								title: res.data.desc,
								duration: 3000,
								icon: "none"
							})
							/* 调转登录页 */
							setTimeout(function() {
								uni.navigateTo({
									url: "login"
								})
							}, 3000)
						} else if(res.data.code==200){
							/* 过期时间 */
							 var expiresDate= new Date();
								expiresDate.setTime(expiresDate.getTime() + (24*15*10*60*1000));	
							uni.setStorageSync('jaAuthorization',res.data.data.token);
							//uni.setStorageSync('japassword',this.userpwd,{expires: expiresDate});
							uni.setStorageSync('jaloginName',res.data.data.userName,{expires: expiresDate});
							uni.setStorageSync('jafullName',res.data.data.fullName);
							uni.setStorageSync('jaUserId',res.data.data.userId);
							uni.setStorageSync('jaUserType',res.data.data.userType);
							uni.setStorageSync('nation',res.data.data.nation);
							uni.setStorageSync('province',res.data.data.province);
							uni.setStorageSync('city',res.data.data.city);
							uni.setStorageSync('county',res.data.data.county); 
							
							
							
							uni.switchTab({
							    url: '../index/index'
							});
						}
					},
					fail(e) {
						uni.hideLoading()
						uni.showToast({
							title: "未知错误",
							duration: 2000,
							icon: "none"

						})
					}
				});


			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200upx;
		width: 200upx;
		margin-top: 200upx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50upx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36upx;
		color: #8f8f94;
	}
</style>
